package com.example.mq.demo;

import com.example.mq.mqclient.Channel;
import com.example.mq.mqclient.Connection;
import com.example.mq.mqclient.ConnectionFactory;
import com.example.mq.mqserver.core.BasicProperties;
import com.example.mq.mqserver.core.ExchangeType;

import java.io.IOException;
import java.util.List;

/**
 * 多虚拟主机功能演示
 */
public class DemoMultiVirtualHost {

    public static void main(String[] args) throws IOException, InterruptedException {
        // 创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(9090);
        
        // 创建连接
        System.out.println("创建连接...");
        Connection connection = factory.newConnection();
        
        // 创建通道
        System.out.println("创建通道...");
        Channel channel = connection.createChannel();
        
        // 获取虚拟主机列表（默认只有default）
        System.out.println("当前虚拟主机列表：");
        List<String> vhosts = channel.getVirtualHostList();
        for (String vhost : vhosts) {
            System.out.println("- " + vhost);
        }
        
        // 创建两个新的虚拟主机
        System.out.println("\n创建新的虚拟主机...");
        boolean created1 = channel.createVirtualHost("test1");
        System.out.println("创建虚拟主机test1: " + (created1 ? "成功" : "失败"));
        
        boolean created2 = channel.createVirtualHost("test2");
        System.out.println("创建虚拟主机test2: " + (created2 ? "成功" : "失败"));
        
        // 再次获取虚拟主机列表
        System.out.println("\n更新后的虚拟主机列表：");
        vhosts = channel.getVirtualHostList();
        for (String vhost : vhosts) {
            System.out.println("- " + vhost);
        }
        
        // 在不同虚拟主机中创建交换机和队列
        System.out.println("\n在不同虚拟主机中创建交换机和队列...");
        
        // 在test1虚拟主机中创建交换机和队列
        ConnectionFactory factory1 = new ConnectionFactory();
        factory1.setHost("127.0.0.1");
        factory1.setPort(9090);
        factory1.setVirtualHostName("test1");


        
        Connection connection1 = factory1.newConnection();
        Channel channel1 = connection1.createChannel();
        
        channel1.exchangeDeclare("exchange1", ExchangeType.DIRECT, true, false, null);
        channel1.queueDeclare("queue1", true, false, false, null);
        channel1.queueBind("queue1", "exchange1", "routing1");
        
        // 在test2虚拟主机中创建交换机和队列
        ConnectionFactory factory2 = new ConnectionFactory();
        factory2.setHost("127.0.0.1");
        factory2.setPort(9090);
        factory2.setVirtualHostName("test2");
        
        Connection connection2 = factory2.newConnection();
        Channel channel2 = connection2.createChannel();
        
        channel2.exchangeDeclare("exchange2", ExchangeType.DIRECT, true, false, null);
        channel2.queueDeclare("queue2", true, false, false, null);
        channel2.queueBind("queue2", "exchange2", "routing2");
        
        // 发布消息到不同的虚拟主机
        System.out.println("\n向不同虚拟主机发送消息...");
        
        // 向test1的交换机发送消息
        String message1 = "这是发送到test1虚拟主机的消息";
        channel1.basicPublish("exchange1", "routing1", new BasicProperties(), message1.getBytes());
        System.out.println("已发送消息到test1虚拟主机: " + message1);
        
        // 向test2的交换机发送消息
        String message2 = "这是发送到test2虚拟主机的消息";
        channel2.basicPublish("exchange2", "routing2", new BasicProperties(), message2.getBytes());
        System.out.println("已发送消息到test2虚拟主机: " + message2);
        
        // 关闭连接
        System.out.println("\n关闭连接...");
        channel1.close();
        connection1.close();
        
        channel2.close();
        connection2.close();
        
        channel.close();
        connection.close();
        
        System.out.println("演示完成!");
    }
} 